package com.git.hui.yinshua.common.email;


import com.git.hui.yinshua.common.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
 * @author yihui
 * @date 24/06/07
 */
@Slf4j
public class EmailWrapper {
    private static volatile String from;

    public static String getFrom() {
        if (from == null) {
            synchronized (EmailWrapper.class) {
                if (from == null) {
                    from = ContextUtil.getEnvironment().getProperty("spring.mail.from", "xhhuiblog@163.com");
                }
            }
        }
        return from;
    }

    /**
     * springboot-email封装的发送邮件
     *
     * @param title
     * @param to
     * @param content
     * @return
     */
    public static boolean sendMail(String title, String to, String content) {
        try {
            JavaMailSender javaMailSender = ContextUtil.getBean(JavaMailSender.class);
            MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(getFrom());
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(title);
            //邮件内容，第二个参数设置为true，支持html模板
            mimeMessageHelper.setText(content, true);
            Thread.currentThread().setContextClassLoader(EmailWrapper.class.getClassLoader());
            javaMailSender.send(mimeMailMessage);
            return true;
        } catch (Exception e) {
            log.warn("sendEmail error {}@{}, {}", title, to, e);
            return false;
        }
    }

    /**
     * commons-mail的封装方式
     *
     * @param title
     * @param to
     * @param content
     * @return
     */
    public static boolean sendEmail(String title, String to, String content) {
        try {
            HtmlEmail email = new HtmlEmail();
            email.setCharset(ContextUtil.getProperty("spring.mail.default-encoding"));
            email.setHostName(ContextUtil.getProperty("spring.mail.host"));
            email.setSmtpPort(25);
            email.setAuthenticator(new DefaultAuthenticator(ContextUtil.getProperty("spring.mail.username"),
                    ContextUtil.getProperty("spring.mail.password")));
            email.setSSLOnConnect(true);
            email.setFrom(ContextUtil.getProperty("spring.mail.from"));
            email.setSubject(title);

            // set the html message
            email.setHtmlMsg(content);

            email.addTo(to);
            email.setTextMsg("邮件客户端不支持html格式邮件!!!");
            Thread.currentThread().setContextClassLoader(EmailWrapper.class.getClassLoader());
            String ans = email.send();
            log.warn("sendEmail success {}@{}, {}", title, to, ans);
            return true;
        } catch (Exception e) {
            log.warn("sendEmail error {}@{}, {}", title, to, e);
            return false;
        }
    }
}
